[id].vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <main>
  3. <div>
  4. <div v-if="pending">
  5. Pending...
  6. </div>
  7. <div v-else>
  8. <h1>{{ $t('download') }}</h1>
  9. <p>Edit :{{ file }}</p>
  10. <UiForm v-if="!pending" :model="File" :entity="file">
  11. <template #form.input="{model, entity: file}">
  12. <UiInputText field="name" v-model="file.name" type="text" :rules="rules.nameRules" />
  13. <UiInputEnum field="status" v-model="file.status" enum="file_status" />
  14. <v-btn @click="cancel" class="ma-5">Annuler</v-btn>
  15. <v-btn @click="save" class="ma-5">Enregistrer</v-btn>
  16. <v-btn @click="deleteAndGoBack" class="ma-5">Supprimer</v-btn>
  17. <v-btn @click="refresh" class="ma-5">Refresh</v-btn>
  18. <v-btn @click="goBack" class="ma-5">Retour</v-btn>
  19. <v-btn to="/poc/display" class="ma-5">Détails</v-btn>
  20. </template>
  21. </UiForm>
  22. <v-btn to="/poc">Retour</v-btn>
  23. </div>
  24. </div>
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import {useEntityManager} from "~/composables/data/useEntityManager";
  29. import {ref, Ref} from "@vue/reactivity";
  30. import File from "~/models/Core/File";
  31. import {useEntityFetch} from "~/composables/data/useEntityFetch";
  32. import {useI18n} from "vue-i18n";
  33. const route = useRoute()
  34. const id: Ref<number> = ref(parseInt(route.params.id as string))
  35. const { em }= useEntityManager()
  36. const { fetch } = useEntityFetch()
  37. const { data: file, pending, refresh } = fetch(File, id.value)
  38. // Get file from store
  39. // const file: ComputedRef<File> = computed( () => {
  40. // return em.find(File, id.value)
  41. // })
  42. // Update store when form is changed
  43. // const onFileChange = () => {
  44. // console.log(file.value)
  45. // em.save(File, file.value)
  46. // }
  47. const i18n = useI18n()
  48. const rules = {
  49. nameRules: [
  50. (nameValue: string) => !!nameValue || i18n.t('required'),
  51. (nameValue: string) => (nameValue || '').length <= 128 || i18n.t('name_length_rule'),
  52. (nameValue: string) => (nameValue || '').length >= 2 || i18n.t('name_length_rule'),
  53. ]
  54. }
  55. const save = async () => {
  56. if (file.value === null) {
  57. throw new Error('File not found')
  58. }
  59. await em.persist(File, file.value)
  60. }
  61. const cancel = async () => {
  62. if (file.value === null) {
  63. throw new Error('File not found')
  64. }
  65. if (em.isNewInstance(File, id.value)) {
  66. await em.delete(File, file.value)
  67. } else {
  68. em.reset(File, file.value)
  69. }
  70. }
  71. const deleteItem = async () => {
  72. if (file.value === null) {
  73. throw new Error('File not found')
  74. }
  75. await em.delete(File, file.value)
  76. }
  77. const goBack = () => {
  78. navigateTo('/poc')
  79. }
  80. const saveAndGoBack = async () => {
  81. await save()
  82. goBack()
  83. }
  84. const cancelAndGoBack = async () => {
  85. await cancel()
  86. goBack()
  87. }
  88. const deleteAndGoBack = async () => {
  89. await deleteItem()
  90. goBack()
  91. }
  92. </script>
  93. <style>
  94. </style>